package com.example.superjson;

import java.util.ArrayList;
 
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.EditText;

public class SetURLActivity extends Activity {

	private EditText ip_editText;
	private EditText table_editText;
	private CheckedTextView col_1;
	private CheckedTextView col_2;
	private CheckedTextView col_3;
	private CheckedTextView col_4;
	private Button finish_button;
	private EditText rownum_editText;
	private ArrayList<Integer> columlist = new ArrayList<Integer>();
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_seturl);
		
		ip_editText = (EditText)findViewById(R.id.ip_edit);
		table_editText = (EditText)findViewById(R.id.table_edit);
		rownum_editText = (EditText)findViewById(R.id.rownum_edit);
		
		col_1 = (CheckedTextView)findViewById(R.id.col_1);
		col_2 = (CheckedTextView)findViewById(R.id.col_2);
		col_3 = (CheckedTextView)findViewById(R.id.col_3);
		col_4 = (CheckedTextView)findViewById(R.id.col_4);
		
		OnClickListener checkedTextListener = new OnClickListener(){
			@Override
			public void onClick(View v) {
				((CheckedTextView)v).toggle();
			}
		};
		
		col_1.setOnClickListener(checkedTextListener);
		col_2.setOnClickListener(checkedTextListener);
		col_3.setOnClickListener(checkedTextListener);
		col_4.setOnClickListener(checkedTextListener);
		
		columlist.add(col_1.getId());
		columlist.add(col_2.getId());
		columlist.add(col_3.getId());
		columlist.add(col_4.getId());
		
		finish_button = (Button)findViewById(R.id.finish_button);
		finish_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				urlGenerate();
				finish();
			}
		});
		SharedPreferences URLSet = getSharedPreferences("URLSet",MODE_PRIVATE);
		String collist = URLSet.getString("collist","ID+UnderFlowDI+Flux");
		int rownum = URLSet.getInt("rowmun", 50);
		for(int i = 0; i<columlist.size(); i++){
			CheckedTextView findCheckedTextViewRef = (CheckedTextView)findViewById(columlist.get(i));
			if(collist.contains(findCheckedTextViewRef.getText())){
				findCheckedTextViewRef.setChecked(true);
			}
		}
		rownum_editText.setText(rownum+"");
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		urlGenerate();
		return super.onKeyDown(keyCode, event);
	}

	@SuppressLint("NewApi")
	private void urlGenerate(){
		SharedPreferences sharedPreferences = getSharedPreferences("URLSet", MODE_PRIVATE);
		SharedPreferences.Editor editor = sharedPreferences.edit();

		editor.putString("IP", ip_editText.getText().toString());
		editor.putString("tablename", table_editText.getText().toString());
		String collist = "";
		for(int i = 0; i<columlist.size(); i++){
			CheckedTextView findCheckedTextViewRef = (CheckedTextView)findViewById(columlist.get(i));
			if(findCheckedTextViewRef.isChecked()){
				collist += findCheckedTextViewRef.getText().toString()+"+";
			}
		}
		editor.putString("collist", collist);
		editor.putInt("rownum", Integer.parseInt(rownum_editText.getText().toString()));
		editor.commit();

	}
	
}
